<?php

namespace app\config;

use Core\Result\Result;
use GuzzleHttp\Client;
use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\PhoneNumber;

class Sms
{
    /**
     * 创客短信
     * @param $phone
     * @param $code
     * @param $type
     * @return mixed
     */
    public function send($phone,$code,$type=1)
    {
        $phone = 手机号切换($phone);
        switch ($type){
            case 1://通用验证码
                $content="【XX】您的验证码：{$code}，您正进行身份验证，打死不告诉别人！";
                break;
            default:
                $content="【XX】您的验证码：{$code}，您正进行身份验证，打死不告诉别人！";
        }
        $content= urlencode($content);
        $url="http://47.105.221.205:7862/sms?action=send&account=922166&password=XQBRz8&mobile=".$phone."&content=".$content."&extno=10690226&rt=json";
        $Client = new Client();
        $resp = $Client->get($url);
        $respData = json_decode($resp->getBody(), true);
        if($respData['status'] != 0) returnData(500,'短信发送失败，请稍后再试~');
        return $respData;
    }
    /**
     * 云市场短信发送
     * http://help.guoyangyun.com/Problem/Qm.html
     * @param string $phone 手机
     *
     * @return void
     */
    public static function cloudSmsSend($phone,$code,$type=1)
    {
        $host = "https://gyytz.market.alicloudapi.com";
        $path = "/sms/smsSend";
        $appcode = "0a2286f6d2254e74a4133bc8e3cef016";
        $headers = [
            "Authorization" => "APPCODE " . $appcode,
            "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"
        ];
        $bodys = "mobile=$phone&param=$code&&smsSignId=7bac7f2a238d44398107722790da7357&templateId=059c8eddea8c42a6b7f56d0725610a5b";
        $url = $host . $path;
        $client = new Client();
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $bodys
        ]);
        $statusCode = $response->getStatusCode();
        var_dump($statusCode);
        if($statusCode == 200){
            $content = $response->getBody()->getContents();
            $json_decode = json_decode($content, true);
            var_dump($json_decode);
        }
    }

    /**
     * 发送手机验证码
     *
     * @param string $phone 手机
     *
     * @return void
     */
    public static function captcha($phone,$code,$type=1)
    {
        switch ($type){
            case 1:
                //$content = '您的验证码：${code}，您正在进行身份验证，请勿泄露于他人！';
                $content = '您的验证码：<b>' . $code . '</b>，您正在进行身份验证，请勿泄露于他人！';
                $template = 'SMS_494905091'; //模板
                $data = ['code' => $code];
                self::easSend($phone, $content, $template, $data);
                break;
            default:
                $content = '您的验证码：<b>' . $code . '</b>，您正在进行身份验证，请勿泄露于他人！';
                $template = 'SMS_494905091'; //模板
                $data = ['code' => $code];
                self::easSend($phone, $content, $template, $data);
        }
    }

    /**
     * 发送短信
     *
     * @param string $phone    手机号码
     * @param string $content  文字内容，使用在以文字内容发送的平台
     * @param string $template 模板ID，使用在以模板ID来发送短信的平台
     * @param string $data     模板变量，使用在以模板ID来发送短信的平台
     * @param string $intcode  国际码，发送国际短信时需要
     *
     * @return void
     */
    public static function easSend($phone, $content = '', $template = '', $data = [], $intcode = '')
    {
        $default = [
            // HTTP 请求的超时时间（秒）
            'timeout' => 5.0,
            // 默认发送配置
            'default' => [
                // 网关调用策略，默认：顺序调用
                'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
                // 默认可用的发送网关
                'gateways' => [],
            ],
            // 可用的网关配置
            'gateways' => [
                'errorlog' => [
                    'file' => __DIR__ . '/easysms/' . date('Ymd') . 'easysms.log',
                ],
            ]
        ];

        try {
            $config = include(__DIR__ . '/easysms.php');
            $easySms = new EasySms($config);
            if ($intcode) {
                // 国际短信
                $number = new PhoneNumber($phone, $intcode);
                $easySms->send($number, [
                    'content' => $content,
                    'template' => $template,
                    'data' => $data,
                ]);
            } else {
                // 国内短信
                $easySms->send($phone, [
                    'content' => $content,
                    'template' => $template,
                    'data' => $data,
                ]);
            }
        } catch (\Exception $e) {
            Result::error('短信发送失败：' . $e->getMessage());
        }
    }
}